package com.example.K501;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Accounts{
	
	public static final String KEY_ROWID = "_id"; //this is going to give us the row id, everytime e put in something to the database, it will create rows
	public static final String KEY_NAME = "account_username"; //value to reference person's name to get the information from the database
	public static final String KEY_EMAIL = "account_email";
	
	private static final String DATABASE_NAME = "501K";
	private static final String DATABASE_TABLE = "accountsTable";
	private static final int DATABASE_VERSION = 1;
	
	private DbHelper ourHelper; //instance of DBHelper class
	private final Context ourContext;
	private SQLiteDatabase ourDatabase;
	
	private static class DbHelper extends SQLiteOpenHelper{

		public DbHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		//this will only be called one time when our db is called
		@Override
		public void onCreate(SQLiteDatabase db) {
			//executing some SQL code
			db.execSQL("CREATE TABLE " + DATABASE_TABLE + " ("  +			
					KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "	+		//setting up the table with first column(Row1, Row2..etc)
					KEY_NAME + " TEXT NOT NULL, " + 							//second column for the name as text
					KEY_EMAIL + " TEXT NOT NULL);" 
			);
		}

		//if the table exists then drop it and call the onCreate method
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}
		
	}
	
	public Accounts(Context c){
		ourContext = c;
	}
	
	public Accounts open() throws SQLException {
		ourHelper = new DbHelper(ourContext);	//get everything set up
		ourDatabase = ourHelper.getWritableDatabase();	//open up our database through the helper
		return this;
	}
	
	public void close(){
		ourHelper.close();
	}

	public long createEntry(String name, String hotness) {
		ContentValues cv = new ContentValues(); //getting everything initialized there
		cv.put(KEY_NAME, name);
		cv.put(KEY_EMAIL, hotness);
		return ourDatabase.insert(DATABASE_TABLE, null, cv); //make sure this table has those two keys above to hold it or it'll fail
	}
}